package com.matt.service.impl;

import com.matt.domain.meeting.Meeting;
import com.matt.domain.meeting.MeetingContent;
import com.matt.domain.meeting.MeetingType;
import com.matt.domain.meeting.MeetingUser;
import com.matt.foundation.DO.MeetingDO;
import com.matt.foundation.query.MeetingQuery;
import com.matt.foundation.utils.DateUtil;
import com.matt.repository.base.MeetingLinkRepository;
import com.matt.repository.meeting.MeetingContentRepository;
import com.matt.repository.meeting.MeetingRepository;
import com.matt.repository.meeting.MeetingTypeRepository;
import com.matt.repository.meeting.MeetingUserRepository;
import com.matt.service.MeetingService;
import com.matt.service.remoteCall.SsoCallService;
import com.matt.service.TeachingAreaService;
import org.assertj.core.util.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;

/**
 * *******************************
 * Created by Ron.
 * form com.matt
 * on 2018/6/28
 * *******************************
 */
@Service
public class MeetingServiceImpl implements MeetingService {
    @Autowired
    private MeetingTypeRepository meetingTypeRepository;

    @Autowired
    private MeetingRepository meetingRepository;

    @Autowired
    private MeetingUserRepository meetingUserRepository;

    @Autowired
    private MeetingContentRepository meetingContentRepository;

    @Autowired
    private MeetingLinkRepository meetingLinkRepository;

    @Autowired
    private SsoCallService ssoCallService;

/*    @Autowired
    private DingDingUtil dingDingUtil;*/

    @Autowired
    private TeachingAreaService teachingAreaService;


   /* @Value("${server.website.ip}")
    private String webSiteUrl;*/

    @Override
    public Page<MeetingDO> getPage(MeetingQuery query) {

        List<MeetingDO> list = meetingLinkRepository.findByCondition(query);
        Long count = meetingLinkRepository.countByCondition(query);
        return new PageImpl<MeetingDO>(list, PageRequest.of(query.getPageNum(), query.getPageSize()), count);
    }

    @Transactional
    @Override
    public Boolean addMeeting(String realName, Meeting meeting, String uids, String startDate) {
        meeting.setStartDate(DateUtil.parseToDateMilis(startDate));
        meetingRepository.save(meeting);
        meetingTypeRepository.getOne(meeting.getMType());
      //  String[] sids = uids.split(",");
        List<MeetingUser> meetingUserList = Lists.newArrayList();
      //  List<String> dingList = ssoCallService.getStaffDTOList(sids).stream().map(o -> o.getDingDingUserId()).collect(Collectors.toList());
/*        if (dingList.size() != 0) {
            String dingIds = dingList.toString().replace("[", "").replace("]", "");
*//*            Boolean result = dingDingUtil.sendMsg(dingIds, String.format(DingDingUtil.MSG_TEMPLETE, realName, startDate, teachingAreaService.getTeachingAreaById(meeting.getTaid()).getName(), meeting.getTitle(), meetingTypeRepository.getOne(meeting.getMType()).getName(), webSiteUrl, meeting.getId()));*//*
            Arrays.stream(sids).forEach(o -> {
                MeetingUser meetingUser = MeetingUser.of(o, meeting.getId());
          *//*      if (result)
                    meetingUser.setDing(true);*//*
                meetingUserList.add(meetingUser);
            });
        }*/

        meetingUserRepository.saveAll(meetingUserList);
        return true;
    }

    @Override
    public List<MeetingType> getAllType() {
        return meetingTypeRepository.findByStatus(0);
    }

    @Override
    public Meeting getById(String id) {
        return meetingRepository.getOne(id);
    }

    @Override
    public List<MeetingContent> getByMeetingId(String id) {
        return meetingContentRepository.findByMid(id);
    }

    @Transactional
    @Override
    public MeetingContent addMeetingContent(String uid, String uname, String mid, String content, String atta) {
        MeetingContent meetingContent = new MeetingContent();
        meetingContent.setUid(uid);
        meetingContent.setUname(uname);
        meetingContent.setMid(mid);
        meetingContent.setContent(content);
        meetingContent.setAtta(atta);
        return meetingContentRepository.save(meetingContent);
    }
}
